home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: undergrad.math.uwaterloo.ca!clgonsal
- From: clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves)
- Subject: Re: Huffman Coding
- Sender: news@undergrad.math.uwaterloo.ca (news spool owner)
- Message-ID: <DMuBJ6.92t@undergrad.math.uwaterloo.ca>
- Date: Thu, 15 Feb 1996 23:21:05 GMT
- References: <4fvtbt$ils@garuda.csulb.edu>
- Nntp-Posting-Host: cayley.uwaterloo.ca
- Organization: University of Waterloo
-
- In article <4fvtbt$ils@garuda.csulb.edu>, Jose Reyes <joreyes@csulb.edu> wrote:
- >Does anyone have any good algorithms for Huffman encoding and decoding?
- >If so could you either post them to the group or send them to me. Thanks.
-
- Pretty well any book on algorithms will have the basic Huffman
- encoding/decoding algorithm which shouldn't be too hard to turn into code.
-
- Encoding consists of generating your table, and then outputiing the correct
- code for each thing in your input stream. The hard part is generating the
- table, which is actually pretty easy. You start of with a node for each
- character in your alphabet. Each node also has a "weight" which is the
- frequency or probability of that character.
-
- Push all your nodes into a priority queue. Extract the two minimum nodes
- and create a new node as their parent. Push that node into the priority
- queue. Repeat n-1 times (where n is the number of leaf-nodes). You now have
- a huffman tree.
-
- From the tree you can generate an encoding table. Each "code" will have one
- bit for each edge along the path from the root to the leaf. use something
- like 0 for left, 1 for right.
-
- To decode you need to recreate the tree (how you do that is up to you...
- you need to transmit either the frequency data, the table, or some other
- encoding of the tree), and then you just take one step down the tree for
- each bit you read in. When you hit a leaf output the code and jump back to
- the root.
-
- --
- Carl Laurence Gonsalves - clgonsal@undergrad.math.uwaterloo.ca
- Computer Science, University of Waterloo
- http://www.undergrad.math.uwaterloo.ca/~clgonsal/
- http://www.csclub.uwaterloo.ca/~clgonsal/
-